package com.yidu.qx.dao;
import com.yidu.qx.domain.QxCharacterEntity;
import com.yidu.qx.domain.QxModuleEntity;
import com.yidu.qx.domain.QxUserEntity;
import com.yidu.qx.utils.Pages;
import org.aspectj.apache.bcel.generic.RETURN;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.jdbc.Work;
import org.springframework.orm.hibernate4.HibernateTemplate;
import org.springframework.util.StringUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: jurisdiction
 * @description
 * @author: 周佳佳
 * @create: 2019-10-03 18:14
 **/
public class CharacterDaoImpl implements  CharacterDao {
    private HibernateTemplate hibernateTemplate;

    public HibernateTemplate getHibernateTemplate() {
        return hibernateTemplate;
    }

    public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
        this.hibernateTemplate = hibernateTemplate;
    }

    @Override
    public void add(QxCharacterEntity qxCharacterEntity) {
        hibernateTemplate.save(qxCharacterEntity);
    }

    @Override
    public void delete(QxCharacterEntity qxCharacterEntity) {
        hibernateTemplate.delete(qxCharacterEntity);
    }

    @Override
    public void update(QxCharacterEntity qxCharacterEntity) {
        hibernateTemplate.update(qxCharacterEntity);
    }

    @Override
    public List<QxCharacterEntity> findAll(QxCharacterEntity qxCharacterEntity, Pages pages) {

        DetachedCriteria criteria = DetachedCriteria.forClass(QxCharacterEntity.class);

        if(!StringUtils.isEmpty(qxCharacterEntity.getChaName())){
            criteria.add(Restrictions.ilike("chaName","%"+qxCharacterEntity.getChaName()+"%"));
        }

        //过滤掉作废的
        //criteria.add(Restrictions.eq("chaState","on"));


        System.out.println("页"+pages.getStartRows());
        System.out.println("行数"+pages.getLimit());

        List<QxCharacterEntity> list  = (List<QxCharacterEntity>) hibernateTemplate.findByCriteria(criteria,pages.getStartRows(),pages.getLimit());
        return list;
    }

    @Override
    public Map<String,Object> index(QxUserEntity qxUserEntity) {
        Session session = hibernateTemplate.getSessionFactory().openSession();
        List<QxModuleEntity> list = new ArrayList<QxModuleEntity>();

        List<QxModuleEntity> list1 = new ArrayList<QxModuleEntity>();

        Map<String,Object> map = new HashMap<>();

        session.doWork(new Work() {
            @Override
            public void execute(Connection connection) throws SQLException {
                String sql = "select md.mod_id,md.t_m_mod_id,md.mod_name,md.mod_comment,md.mod_state,md.MOD_SITE from QX_ROLEUSER ru,qx_roles_modules rm,qx_user us,qx_module md,qx_character qc  \n" +
                        "where us.user_id = ru.user_id and ru.cha_id = rm.cha_id and rm.mod_id = md.mod_id and rm.cha_id = qc.cha_id and us.user_id='"+qxUserEntity.getUserId()+"' and md.MOD_STATE='on' and qc.cha_STATE='on' group by md.mod_id,md.t_m_mod_id,md.mod_name,md.mod_comment,md.mod_state,md.MOD_SITE\n";
                System.out.println("哈哈哈"+sql);
                PreparedStatement pstm = connection.prepareStatement(sql);


                ResultSet rs = pstm.executeQuery();
                while(rs.next()){
                    QxModuleEntity qxModuleEntity = new QxModuleEntity();
                    qxModuleEntity.setModId(rs.getString("mod_id"));
                    qxModuleEntity.setModName(rs.getString("mod_name"));
                    qxModuleEntity.setTmModId(rs.getString("t_m_mod_id"));
                    qxModuleEntity.setModComment(rs.getString("mod_state"));
                    qxModuleEntity.setModSite(rs.getString("MOD_SITE"));
                    list.add(qxModuleEntity);
                }

                System.out.println("集合的大小"+list.size());

                sql = "select mo.* from qx_module mo where mod_id in(\n" +
                        "select md.t_m_mod_id from QX_ROLEUSER ru,qx_roles_modules rm,qx_user us,qx_module md,qx_character qc\n" +
                        "where us.user_id = ru.user_id and ru.cha_id = rm.cha_id and rm.mod_id = md.mod_id and rm.cha_id = qc.cha_id and us.user_id=? and md.MOD_STATE='on' and qc.cha_STATE='on') and mo.MOD_STATE='on'";

                System.out.println("xixixi"+sql);

                pstm = connection.prepareStatement(sql);
                pstm.setString(1,qxUserEntity.getUserId());

                rs = pstm.executeQuery();
                while(rs.next()){
                    QxModuleEntity qxModuleEntity = new QxModuleEntity();
                    qxModuleEntity.setModId(rs.getString("mod_id"));
                    qxModuleEntity.setModName(rs.getString("mod_name"));
                    qxModuleEntity.setTmModId(rs.getString("t_m_mod_id"));
                    qxModuleEntity.setModComment(rs.getString("mod_state"));
                    list1.add(qxModuleEntity);
                }

                map.put("one",list);
                map.put("two",list1);

                rs.close();
                pstm.close();
            }
        });

        session.close();
        return map;
    }

    @Override
    public QxCharacterEntity findById(String id) {
        return hibernateTemplate.get(QxCharacterEntity.class, id);
    }
    @Override
    public List<QxCharacterEntity> findAllone() {
        DetachedCriteria datechedCriteria= DetachedCriteria.forClass(QxCharacterEntity.class);
        datechedCriteria.add(Restrictions.eq("chaState","on"));
        List<QxCharacterEntity> list= (List<QxCharacterEntity>) hibernateTemplate.findByCriteria(datechedCriteria);
        return list;
    }
    @Override
    public Long count(QxCharacterEntity qxCharacterEntity) {
        DetachedCriteria criteria = DetachedCriteria.forClass(QxCharacterEntity.class);
        criteria.setProjection(Projections.rowCount());

        if(!StringUtils.isEmpty(qxCharacterEntity.getChaName())){
            criteria.add(Restrictions.ilike("chaName","%"+qxCharacterEntity.getChaName()+"%"));
        }

        //过滤掉作废的
        //criteria.add(Restrictions.eq("chaState","on"));

        Long count = (Long)hibernateTemplate.findByCriteria(criteria).get(0);

        return count;
    }
}
